<?php

namespace app\common\model;

use think\Model;
use think\Session;

class Admin extends Model
{
    protected $name = 'admin';

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    protected $append = [
        'group_text',  // 角色的名称
        'avatar_text',
    ];

    /**
     * 重置用户密码
     * @author baiyouwen
     */
    public function resetPassword($uid, $NewPassword)
    {
        $passwd = $this->encryptPassword($NewPassword);
        $ret = $this->where(['id' => $uid])->update(['password' => $passwd]);
        return $ret;
    }

    // 密码加密
    protected function encryptPassword($password, $salt = '', $encrypt = 'md5')
    {
        return $encrypt($password . $salt);
    }
    
    /**
     * 获取个人头像信息  获取器
     * @param string $value
     * @param array  $data
     * @return string
     * get + AvatarCdn + Attr
     */
    public function getAvatarTextAttr($value, $data)
    {
        $avatar = $data['avatar'] ?: '';

        if(!$avatar || !is_file('.' . $avatar))
        {
            $avatar = '/assets/img/avatar.png';
        }

        // 获取当前域名
        $domain = request()->domain();

        return $domain . $avatar;
    }
}
